package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerfilecolumnDAO")
@Transactional
public class CrawlerfilecolumnDAOImpl extends AbstractJpaDao<Crawlerfilecolumn> implements CrawlerfilecolumnDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfilecolumn.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerfilecolumnDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnById(Integer id) throws DataAccessException {

		return findCrawlerfilecolumnById(id, -1, -1);
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilecolumnById", startResult, maxRows, id);
			return (Crawlerfilecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByName(String name) throws DataAccessException {

		return findCrawlerfilecolumnByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilecolumnByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByNameContaining(String name) throws DataAccessException {

		return findCrawlerfilecolumnByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilecolumnByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByDescription(String description) throws DataAccessException {

		return findCrawlerfilecolumnByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilecolumnByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfilecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerfilecolumnByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilecolumnByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerfilecolumn>(query.getResultList());
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfilecolumnByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilecolumnByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfilecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilecolumnsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilecolumn> findAllCrawlerfilecolumns() throws DataAccessException {

		return findAllCrawlerfilecolumns(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findAllCrawlerfilecolumns(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilecolumns", startResult, maxRows);
		return new LinkedHashSet<Crawlerfilecolumn>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Crawlerfilecolumn>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfilecolumn> findCrawlerfilecolumnsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilecolumn.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerfilecolumn> crawlerfilecolumns = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerfilecolumns;
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findCrawlerfilecolumnByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawlerfilecolumn findCrawlerfilecolumnByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilecolumnByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Crawlerfilecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfilecolumn entity) {
		return true;
	}
}
